﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TestcraftLibrary
{
    public class ChunkIndex : IDisposable
    {
        private System.IO.Stream stream;
        private Dictionary<Point, UInt64> chunks;

        public ChunkIndex(string path)
        {
            this.stream = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate);
            this.Initialize();
        }

        private void Initialize()
        {
            System.IO.BinaryReader reader = new System.IO.BinaryReader(this.stream);
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                Int32 x = reader.ReadInt32();
                Int32 y = reader.ReadInt32();
                UInt64 offset = reader.ReadUInt64();
                this.chunks.Add(new Point(x, y), offset);
            }
        }

        public bool ChunkExists(Point p)
        {
            return this.chunks.ContainsKey(p);
        }

        public UInt64 GetChunkOffset(Point p)
        {
            return this.chunks[p];
        }


        public void Dispose()
        {
            this.stream.Close();
            this.stream.Dispose();
        }
    }
}
